package com.example.developerlab;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CustomizedDialog extends Dialog {
    private static final int INVALID_LAYOUT_ID = -1;
    /**
     * 弹窗标题控件
     */
    private TextView titleTv;

    /**
     * 提示信息控件
     */
    private TextView warningMsgTv;

    /**
     * 左侧按钮
     */
    private Button leftBtn;

    /**
     * 右侧按钮
     */
    private Button rightBtn;

    /**
     * 弹窗标题
     */
    private String titleMsg;

    /**
     * 提示信息
     */
    private String warningMsg;
    /**
     * 左侧按钮文字
     */
    private String leftBtnMsg;
    /**
     * 右侧按钮文字
     */
    private String rightBtnMsg;

    /**
     * 弹窗只有一个按钮？
     */
    private boolean isSingleBtn = false;

    /**
     * 弹窗按钮的回调
     */
    private OnDialogClickListener onClickBottomListener;

    /**
     * 弹窗自定义layout，一定要定义以下控件：
     * tv_title：弹窗标题
     * tv_message：提示信息
     * btn_left：左侧按钮
     * btn_right：右侧按钮
     */
    private int customizedDialogLayout = INVALID_LAYOUT_ID;

    public CustomizedDialog(Context context) {
        super(context);
    }

    public static void initCustomDialogNew(Context context) {
        final CustomizedDialog dialog = new CustomizedDialog(context);
        dialog.setTitleMsg("温馨提示")
                .setWarningMsg("这是是一个测试弹窗显示信息内容的文本。多来几行看看长度如何。")
                .setOnDialogClickListener(new OnDialogClickListener() {
                    @Override
                    public void onRightClick() {
                        dialog.dismiss();
                        Toast.makeText(context, "xxxx", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onLeftClick() {
                        dialog.dismiss();
                        Toast.makeText(context, "ssss", Toast.LENGTH_SHORT).show();
                    }
                }).show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 如果自定义布局有效，则使用自定义布局
        setContentView(customizedDialogLayout == INVALID_LAYOUT_ID ?
                R.layout.layout_custom_dialog : customizedDialogLayout);

        // 将弹窗背景色设置成透明
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        // 弹窗外部蒙层不可取消弹窗
        setCanceledOnTouchOutside(false);

        titleTv = findViewById(R.id.tv_title);
        if (!TextUtils.isEmpty(titleMsg)) {
            titleTv.setText(titleMsg);
        }

        warningMsgTv = findViewById(R.id.tv_message);
        warningMsgTv.setText(warningMsg);

        rightBtn = findViewById(R.id.btn_right);
        if (!TextUtils.isEmpty(rightBtnMsg)) {
            rightBtn.setText(rightBtnMsg);
        }
        rightBtn.setOnClickListener(v -> {
            if (onClickBottomListener != null) {
                onClickBottomListener.onRightClick();
            }
        });

        leftBtn = findViewById(R.id.btn_left);
        if (!TextUtils.isEmpty(leftBtnMsg)) {
            leftBtn.setText(leftBtnMsg);
        }

        leftBtn.setOnClickListener(v -> {
            if (onClickBottomListener != null) {
                onClickBottomListener.onLeftClick();
            }
        });

        if (isSingleBtn) {
            leftBtn.setVisibility(View.GONE);
        }
    }

    public CustomizedDialog setOnDialogClickListener(OnDialogClickListener onClickBottomListener) {
        this.onClickBottomListener = onClickBottomListener;
        return this;
    }

    public CustomizedDialog setTitleMsg(String titleMsg) {
        this.titleMsg = titleMsg;
        return this;
    }

    public CustomizedDialog setWarningMsg(String warningMsg) {
        this.warningMsg = warningMsg;
        return this;
    }

    public CustomizedDialog setRightBtnMsg(String rightBtnMsg) {
        this.rightBtnMsg = rightBtnMsg;
        return this;
    }

    public CustomizedDialog setLeftBtnMsg(String leftBtnMsg) {
        this.leftBtnMsg = leftBtnMsg;
        return this;
    }

    public CustomizedDialog setSingleBtn(boolean singleBtn) {
        isSingleBtn = singleBtn;
        return this;
    }

    public CustomizedDialog setCustomizedLayout(int layoutId) {
        customizedDialogLayout = layoutId;
        return this;
    }

    public interface OnDialogClickListener {
        /**
         * 点击左侧按钮
         */
        void onLeftClick();

        /**
         * 点击右侧按钮
         */
        void onRightClick();
    }
}
